package cn.chenfeifx.core.controls.skins;

import cn.chenfeifx.core.common.ClipUtil;
import cn.chenfeifx.core.controls.CFImage;
import javafx.scene.control.SkinBase;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Region;

public class CFImageSkin extends SkinBase<CFImage> {

    private final CFImage cfImage;

    public CFImageSkin(CFImage cfImage) {
        super(cfImage);
        this.cfImage = cfImage;
        getChildren().add(new ImageRegion());
    }

    /**
     * 图片容器
     */
    public class ImageRegion extends Region {

        private final ImageView imageView;

        public ImageRegion() {
            this.imageView = new ImageView();
            this.imageView.imageProperty().bind(cfImage.imageProperty()); // 图片绑定
            initialize();
        }

        private void initialize() {
            // 监听位置大小变化
            heightProperty().addListener((observable, v1, v2) -> layoutChange());
            widthProperty().addListener((observable, v1, v2) -> layoutChange());
            imageView.imageProperty().addListener((observable, oldValue, newValue) -> layoutChange());
            cfImage.fitProperty().addListener((observable, v1, v2) -> layoutChange(v2));
            // 添加到容器
            getChildren().add(imageView);
            // 圆角绑定
            ClipUtil.clipRect(this, cfImage.arcProperty());
        }

        private Image getImage() {
            return cfImage.getImage();
        }

        private void layoutChange() {
            layoutChange(null);
        }

        /**
         * 更改布局方式
         */
        private void layoutChange(CFImage.Fit fit) {
            if (getImage() == null){
                return;
            }
            double width = getWidth();
            double height = getHeight();
            switch (fit == null ? cfImage.fitProperty().get() : fit) {
                case CONTAIN -> {
                    imageView.setPreserveRatio(true);
                    imageView.setFitHeight(height);
                    imageView.setFitWidth(width);
                    imageView.setX(width / 2 - imageView.getLayoutBounds().getWidth() / 2);
                    imageView.setY(height / 2 - imageView.getLayoutBounds().getHeight() / 2);
                }
                case COVER -> {
                    imageView.setPreserveRatio(true);
                    double max = Math.max(width, height);
                    if (getImage().getWidth() < getImage().getHeight()) {
                        imageView.setFitWidth(max);
                        imageView.setFitHeight(0);
                    } else if (getImage().getWidth() > getImage().getHeight()) {
                        imageView.setFitHeight(max);
                        imageView.setFitWidth(0);
                    } else {
                        imageView.setFitHeight(max);
                        imageView.setFitWidth(max);
                    }
                    imageView.setX(width / 2 - imageView.getLayoutBounds().getWidth() / 2);
                    imageView.setY(height / 2 - imageView.getLayoutBounds().getHeight() / 2);
                }
                case FILL -> {
                    imageView.setPreserveRatio(false);
                    imageView.setFitHeight(height);
                    imageView.setFitWidth(width);
                    imageView.setX(0);
                    imageView.setY(0);
                }
                case NONE -> {
                    imageView.setPreserveRatio(true);
                    imageView.setFitHeight(0); // <= 0 使用原图的固有尺寸
                    imageView.setFitWidth(0);
                    imageView.setX(width / 2 - getImage().getWidth() / 2);
                    imageView.setY(height / 2 - getImage().getHeight() / 2);
                }
                case SCALE_DOWN -> {
                    boolean b = width > getImage().getWidth() && height > getImage().getHeight();
                    layoutChange(b ? CFImage.Fit.NONE : CFImage.Fit.CONTAIN);
                }
            }
        }
    }

}
